<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialogHeihei"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles" style="width: 100%">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="{ row } ">
                  <el-button size="small" type="success" @click="hAssign(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除 </el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="total,sizes,prev,pager,next,jumper"
                :current-page="q.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="q.pagesize"
                :total="total"
                @current-change="hCurrentChange"
                @size-change="hSizeChange"
              />

            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :visible.sync="showDialog"
        :title="isEdit ? '编辑角色' : '新增角色' "
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog = false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog
        :visible.sync="showDialogAssign"
        title="权限分配"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <assign-permission v-if="showDialogAssign" :id="id" @close="showDialogAssign = false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addRoles, getRoles, updateRoles } from '@/api/roles'
import { delRoles } from '@/api/roles'
import AssignPermission from './assignPermission.vue'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      q: {
        page: 1,
        pagesize: 2
      },
      roles: [],
      total: 0,
      showDialog: false,
      showDialogAssign: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      isEdit: false,
      id: ''
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.q)
        // console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (e) {
        console.log(e)
      }
    },
    hCurrentChange(page) {
      this.q.page = page
      this.loadRoles()
    },
    hSizeChange(pagesize) {
      this.q.pagesize = pagesize
      this.loadRoles()
    },
    async delRoles(id) {
      const result = await this.$confirm('你确定要删除吗？', '提示', {
        type: 'warning'
      }).catch(e => e)
      // console.log(result)
      // .then(async() => {
      //   const res = await delRoles(id)
      //   // console.log(res)
      //   this.$message.success(res.message)
      // })
      // .catch(e => {
      //   console.log(e)
      //   // this.$message.error(e.message)
      // })
      if (result !== 'confirm') return
      try {
        const res = await delRoles(id)
        // console.log(res)
        this.$message.success(res.message)
        if (this.roles.length === 1 && this.q.page > 1) {
          this.q.page--
        }
        this.loadRoles()
      } catch (e) {
        console.log(e)
      }
    },
    hDel(id) {
      this.delRoles(id)
    },
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.addSubmit()
      })
    },
    async addSubmit() {
      try {
        const res = await addRoles(this.roleForm)
        // console.log(res)
        if (this.total % this.q.pagesize === 0) {
          this.total++
        }
        this.q.page = Math.ceil(this.total / this.q.pagesize)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadRoles()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hEdit(row) {
      // console.log(row)
      this.roleForm = { ...row }
      this.showDialog = true
      this.isEdit = true
    },
    showDialogHeihei() {
      this.showDialog = true

      this.isEdit = false
    },
    async doEdit() {
      try {
        const res = await updateRoles(this.roleForm)
        // console.log(res)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadRoles()
      } catch (e) {
        console.log(e)
      }
    },
    hClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.clearValidate()
    },
    hAssign(id) {
      this.showDialogAssign = true
      this.id = id
    }
  }
}
</script>
